<template>
  <div class="subjects">
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-form ref="subjectsForm" :model="subjectsForm" label-width="80px">
            <el-form-item label="学科名称">
              <el-input v-model="subjectsForm.name"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="17" class="searchBtn">
          <el-button @click="clearContent">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="success" icon="el-icon-edit" class="addSub" @click="addSubjects">新增学科</el-button>
        </el-col>
      </el-row>
      <el-alert :title="`数据一共${allTotals}条`" type="info" show-icon :closable="false"></el-alert>
      <el-table :data="subjectsData" style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="学科名称"></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="创建日期" width="180">
          <template slot-scope="scope">{{scope.row.addDate | format }}</template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template slot-scope="scope">{{scope.row.isFrontDisplay ? '是':'否'}}</template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录"></el-table-column>
        <el-table-column prop="tags" label="标签"></el-table-column>
        <el-table-column prop="totals" label="题目数量"></el-table-column>
        <el-table-column label="操作" width="260">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="$router.push({name:'subjects-directorys',query:{id:scope.row.id,name:scope.row.subjectName}})"
              :underline="false"
            >学科分类</el-link>
            <!--  -->
            <el-link
              type="primary"
              :underline="false"
              @click="$router.push({name:'subjects-tags',query:{id:scope.row.id,name:scope.row.subjectName}})"
            >学科标签</el-link>
            <el-link type="primary" :underline="false" @click="editDialogShow(scope.row)">修改</el-link>
            <el-link type="primary" :underline="false" @click="deleteSubject(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="requestData.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="requestData.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="allTotals"
      ></el-pagination>
    </el-card>
    <!-- 新增或者修改学科弹出框 -->
    <el-dialog
      :title="isEdit? '修改学科':'新增学科'"
      :visible.sync="addDialogVisible"
      @close="closeVisble"
      @open="showData"
    >
      <!-- 添加或者修改学科组件 -->
      <subjects-add
        @close="addDialogVisible=false"
        @loadSubjectList="loadSubjectList()"
        ref="subjectAdd"
        :isEdit="isEdit"
        :addForm="showSubjects"
        :userID="userID"
      ></subjects-add>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求学科列表的组件
import { getSubjectList, subjectsMessage, deleteSubject } from '@/api/hmmm/subjects.js'
// 导入格式化时间的组件
import { parseTime } from '@/filters/index'
// 导入对话框内容组件
import SubjectsAdd from '../components/subjects-add'

export default {
  name: 'Subjects',
  data() {
    return {
      subjectsForm: {}, // 表单的数据对象
      allTotals: 0, // 提示条数 总条数
      subjectsData: [], // 数据列表
      requestData: {
        page: 1,
        pagesize: 10,
        subjectName: '',
      }, // 请求数据的对象
      addDialogVisible: false, // 控制弹出框的显示与隐藏
      isEdit: false,
      showSubjects: {}, // 回显数据
      userID: 0,
    }
  },
  props: {},
  components: {
    SubjectsAdd,
  },
  filters: {
    format(value) {
      return parseTime(value)
    },
  },
  methods: {
    // 请求数据渲染列表
    async loadSubjectList() {
      try {
        // 发送请求
        const { data: res } = await getSubjectList(this.requestData)
        this.subjectsData = res.items
        this.allTotals = res.counts
      } catch (error) {
        console.log(error)
        this.$message.error('获取列表失败')
      }
    },
    // 当每页条数发生改变时触发
    handleSizeChange(val) {
      this.requestData.pagesize = val
      // 调用请求数据函数刷新页面
      this.loadSubjectList()
    },
    // 页码改变时会触发
    handleCurrentChange(val) {
      this.requestData.page = val
      // 调用请求数据函数刷新页面
      this.loadSubjectList()
    },
    // 清除输入框内的搜索内容
    clearContent() {
      this.subjectsForm.name = ''
      this.requestData.page = 1
      this.requestData.pagesize = 10
      this.search()
    },
    // 搜索内容
    search() {
      this.requestData.subjectName = this.subjectsForm.name
      this.loadSubjectList()
    },
    // 新增学科 弹出对话框
    addSubjects() {
      this.addDialogVisible = true
      this.isEdit = false
    },
    // 关闭对话框重置表单数据
    closeVisble() {
      // 触发子组件的关闭事件
      this.$refs.subjectAdd.closeVisible()
      this.showSubjects = {}
    },
    // 显示修改学科对话框
    editDialogShow(row) {
      this.userID = row.id
      this.addDialogVisible = true
      this.isEdit = true
    },
    // 删除科学列表
    async deleteSubject(id) {
      try {
        const data = await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        // 发送删除请求删除数据
        const { data: res } = await deleteSubject(id)
        // 删除成功 提示
        if (res.success) {
          this.$message.success('删除成功')
        } else {
          this.$message.error('删除失败')
        }
        // 刷新列表
        this.loadSubjectList()
      } catch (error) {
        this.$message.info('取消删除操作')
      }
    },
    // 弹出框的open事件 请求数据
    async showData() {
      if (this.isEdit) {
        const { data: res } = await subjectsMessage(this.userID)
        if (res.isFrontDisplay) {
          res.isFrontDisplay = true
        } else {
          res.isFrontDisplay = false
        }
        this.showSubjects = res
      }
    },
  },
  computed: {},
  created() {
    // 加载数列表
    this.loadSubjectList()
  },
}
</script>

<style scoped lang="less">
.el-form-item__label {
  font-size: 30px;
}
/* .el-form {
  display: flex;
} */
.searchBtn {
  margin-left: 12px;
}
.addSub {
  float: right;
}
.el-table {
  margin-top: 10px;
}
.cell {
  display: flex;
  justify-content: space-between;
}
.el-link.el-link--primary {
  margin-right: 8px;
}
.el-pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
.subjects {
  /deep/ .el-dialog {
    width: 30% !important;
  }
}
</style>
